home *** CD-ROM | disk | FTP | other *** search
/ Freelog 117 / FreelogNo117-OctobreNovembre2013.iso / Programmation / jedit / jedit5.1.0install.exe / {app} / macros / Emacs / Emacs_Fixup_Whitespace.bsh < prev    next >
Text File  |  2013-07-28  |  3KB  |  93 lines

  1. /**
  2.  * Emulate GNU Emacs's (fixup-whitespace) function (typically bound to
  3.  * Ctrl-SPACE. Not a complete emulation. Emacs docs for this function:
  4.  *
  5.  *     Fixup white space between objects around caret.
  6.  *     Leave one space or none, according to the context.
  7.  */
  8.  
  9. source (MiscUtilities.constructPath(dirname(scriptPath), "EmacsUtil.bsh"));
  10.  
  11. void emacsFixupWhitespace()
  12. {
  13.     caret = textArea.getCaretPosition();
  14.     caretLine = textArea.getCaretLine();
  15.     lineStart = textArea.getLineStartOffset (caretLine);
  16.     lineEnd = textArea.getLineEndOffset (caretLine);
  17.            
  18.     // Determine whether there's white space at the caret.
  19.     
  20.     ch = charAtCaret();
  21.     chPrev = '\0';
  22.     if (caret > lineStart)
  23.         chPrev = charAt (caret - 1);
  24.  
  25.     if (Character.isWhitespace (ch) || Character.isWhitespace (chPrev))
  26.     {
  27.         // Collapse white space behind and in front of caret, leaving just
  28.         // one. NOTE: For compatibility with Emacs,p if the caret is in the
  29.         // first column, we don't leave any spaces.
  30.  
  31.         // First, the characters behind the caret:
  32.         
  33.         if (Character.isWhitespace (chPrev))
  34.         {
  35.             origCaret = caret;
  36.             caret = caret--;
  37.             textArea.goToPrevCharacter (false);
  38.             chPrev = charAtCaret();
  39.             while ((caret > lineStart) && (Character.isWhitespace (chPrev)))
  40.             {
  41.                 textArea.delete();
  42.                 caret = caret - 1;
  43.                 textArea.goToPrevCharacter (false);
  44.                 chPrev = charAtCaret();
  45.             }
  46.             
  47.             // Caret is now one character behind where we want it.
  48.             
  49.             textArea.goToNextCharacter (false);
  50.         }
  51.  
  52.         if (Character.isWhitespace (ch) && (ch != '\n'))
  53.         {
  54.             ch = charAtCaret();
  55.             while ((caret <= lineEnd) &&
  56.                 (Character.isWhitespace (ch)) &&
  57.                 (ch != '\n'))
  58.             {
  59.                 textArea.delete();
  60.                 ch = charAtCaret();
  61.                 caret = textArea.getCaretPosition();
  62.             }
  63.  
  64.             // Make sure there's one blank left--unless:
  65.             //
  66.             // a) we're in column 1, or
  67.             // b) the next character is not a letter or digit, or
  68.             // c) the previous character is not a letter or digit.
  69.  
  70.            if (caret > lineStart)
  71.             {
  72.                 chPrev = charAt (caret - 1);
  73.  
  74.                 if (Character.isLetterOrDigit (ch) &&
  75.                     Character.isLetterOrDigit (chPrev))
  76.                 {
  77.                     buffer.insert (caret, " ");
  78.                 }
  79.             }
  80.         }
  81.     }
  82.  
  83.     else if ((caret + 1) != lineEnd)
  84.     {
  85.         // Insert one blank.
  86.  
  87.         buffer.insert (caret, " ");
  88.     }
  89. }
  90.  
  91. emacsFixupWhitespace();
  92.  
  93.